<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of menuAction
 * 菜单管理
 * @author xlp
 */
class menuAction extends commonAction {

    protected $menuFile = 'weixin/wxmenu'; //缓存菜单名文件

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    /*
     * 默认首页 
     */

    function index() {
        $menuData = array();
        $userInfo = F('weixin/setting');
        if (isHave($userInfo['user_info'])) {
            $userInfo = $userInfo['user_info'];
        }
        if (isHave($userInfo['wx_AppId']) && isHave($userInfo['wx_AppSecret'])) {
            T('weixin/wxmenu');
            wxmenu::init(admin::$adminInfo['aid'], $userInfo['wx_AppId'], $userInfo['wx_AppSecret']);
            $menuData = wxmenu::getmenu();
            if (isset($menuData['menu']['button'])) {
                $menuData = $menuData['menu']['button'];
            }
        }
        $this->assign(array('rs' => $userInfo, 'data' => $menuData, 'keywords' => D('weixin_reply')->field('keyword')->where(array('status' => 1, 'aid' => admin::$adminInfo['aid']))->order('id DESC')->findAll()));
        $this->display();
    }

    /*
     * 同步菜单到微信服务器上
     */

    function save() {
        $userInfo = F('weixin/setting');
        if (isHave($userInfo['user_info'])) {
            $userInfo = $userInfo['user_info'];
        }
        if (!isHave($userInfo['wx_AppId']) || !isHave($userInfo['wx_AppSecret'])) {
            $this->JsonReturn('请先绑定开发者信息');
        }
        //初始化处理类
        T('weixin/wxmenu');
        wxmenu::init(admin::$adminInfo['aid'], $userInfo['wx_AppId'], $userInfo['wx_AppSecret']);

        $isClose = $this->_postid('isclose', 0);
        $mainName = $this->_post('mainname');
        $mainType = $this->_post('maintype');
        $mainKey = $this->_post('mainkey');
        $mainUrl = $this->_post('mainurl', '', 'trim');
        $smallName = $this->_post('name');
        $smallType = $this->_post('type');
        $smallKey = $this->_post('key');
        $smallUrl = $this->_post('url', '', 'trim');
        $menuData = array();
        $keyName = array();
        //如果关闭菜单则删除菜单并返回结果
        if ($isClose) {
            $result = wxmenu::delmenu();
            if ($result) {
                $this->JsonReturn('操作成功', null, 1);
            } else {
                $this->JsonReturn(wxmenu::getErrorMsg());
            }
        }

        if ($mainName && is_array($mainName)) {
            foreach ($mainName as $key => $nameList) {
                $sub_button = array();
                if ($nameList) {
                    //子菜单处理
                    if (isHave($smallName[$key]) && is_array($smallName[$key])) {
                        foreach ($smallName[$key] as $k => $n) {
                            $stype = isHave($smallType[$key][$k]) && $smallType[$key][$k] == 2 ? 'view' : 'click';
                            $skey = isHave($smallKey[$key][$k]) ? $smallKey[$key][$k] : '';
                            $surl = isHave($smallUrl[$key][$k]) ? $smallUrl[$key][$k] : '';
                            $keyNameValue = $stype == 'view' ? $surl : $skey;
                            $sub_button[] = array(
                                'name' => $n,
                                'type' => $stype,
                                'key' => $skey,
                                'url' => $surl
                            );
                            if ($keyNameValue) {
                                $keyName[md5($keyNameValue)] = $n;
                            }
                        }
                    }
                    //主菜单处理
                    $mtype = isHave($mainType[$key]) && $mainType[$key] == 2 ? 'view' : 'click';
                    $mkey = isHave($mainKey[$key]) ? $mainKey[$key] : '';
                    $murl = isHave($mainUrl[$key]) ? $mainUrl[$key] : '';
                    $keyNameValue = $mtype == 'view' ? $murl : $mkey;
                    $menuData[] = array(
                        'name' => $nameList,
                        'type' => $mtype,
                        'key' => $mkey,
                        'url' => $murl,
                        'sub_button' => $sub_button
                    );
                    if ($keyNameValue) {
                        $keyName[md5($keyNameValue)] = $nameList;
                    }
                }
            }
        }
        $wxMenuData = F($this->menuFile);
        if ($wxMenuData) {
            $wxMenuData = array_merge($wxMenuData, $keyName);
        } else {
            $wxMenuData = $keyName;
        }
        F($this->menuFile, $wxMenuData);
        wxmenu::setMenuData($menuData);
        if (wxmenu::setmenu()) {
            $this->JsonReturn('操作成功', null, 1);
        } else {
            $this->JsonReturn(wxmenu::getErrorMsg());
        }
    }

    /*
     * 菜单使用情况统计
     */

    function stat() {
        $stime = $this->_get('stime', date('Y-m-d'));
        $etime = $this->_get('etime', date('Y-m-d'));
        $where = '';
        if ($stime && $etime) {
            $where = ' AND create_time>=' . strtotime($stime) . ' AND create_time<=' . (strtotime($etime) + (60 * 60 * 24) - 1);
        }
        $sql = "select content,count(id) AS total FROM __TABLE__ WHERE msg_type=1 " . $where . " AND aid=" . admin::$adminInfo['aid'] . " GROUP BY content";
        $totalRs = M('access_log')->query($sql);
        $total = 0;
        $rs = array();
        $keyName = F($this->menuFile);
        foreach ($totalRs as $key => $val) {
            $name = md5(str_replace('&amp;', '&', $val['content']));
            $totalRs[$key]['title'] = isset($keyName[$name]) ? $keyName[$name] : $val['content'];
            $rs[] = $totalRs[$key];
            $total+=$val['total'];
        }
        unset($totalRs);
        if (!$stime || !$etime) {
            //开始日期
            $stime = '';
            //结束日期
            $etime = '';
        }
        $this->assign(array('stime' => $stime, 'etime' => $etime));
        $this->assign(array('total' => $total, 'totalRs' => $rs));
        $this->display();
    }

}
